function AMapWX(a) {
  this.key = a.key, this.requestConfig = {
    key: a.key,
    s: "rsx",
    platform: "WXJS",
    appname: a.key,
    sdkversion: "1.2.0",
    logversion: "2.0"
  }
}

//获取位置经纬度，可以改成从服务器端获取，然后高德地图就可以定位
AMapWX.prototype.getWxLocation = function (a, b) {
    wx.getLocation({
      type: "gcj02",
      success: function (a) {
        // console.log("getWxLocation位置请求调用成功")
        var c = a.longitude + "," + a.latitude;
        // var c = "118.5977" + ",39.2068"
        console.log("getWxLocation获取的坐标: " + c)
        wx.setStorage({
            key: "userLocation",
            data: c
          }),
          // b是传进来的回调函数，c相当于a.location
          b(c)
      },
      fail: function (c) {
        //位置请求调用失败2,看看有没有保存过
        wx.getStorage({
          key: "userLocation",
          success: function (a) {
            a.data && b(a.data)
          }
        }), a.fail({
          errCode: "0",
          errMsg: c.errMsg || ""
        })
      }
    })
  },
  // 根据坐标获取位置名称，如果输入坐标为空则getWxLocation。
  AMapWX.prototype.getRegeo = function (a) {
    // console.log("执行getRegeo 传递的参数" + a)

    //内嵌函数，但是还没
    function c(c) {
      // console.log("执行函数c")
      var d = b.requestConfig;
      wx.request({
        url: "https://restapi.amap.com/v3/geocode/regeo",
        data: {
          key: b.key,
          location: c, //经纬度传过来
          extensions: "all",
          s: d.s,
          platform: d.platform,
          appname: b.key,
          sdkversion: d.sdkversion,
          logversion: d.logversion
        },
        method: "GET",
        header: {
          "content-type": "application/json"
        },
        success: function (b) {
          var d, e, f, g, h, i, j, k, l;
          b.data.status && "1" == b.data.status ? (d = b.data.regeocode,
            // console.log("输出返回的数据b::" + b.data.regeocode.pois[0]),
            e = d.addressComponent, f = [], g = "", d && d.roads[0] && d.roads[0].name && (g = d.roads[0].name + "附近"), h = c.split(",")[0], i = c.split(",")[1], d.pois && d.pois[0] && (g = d.pois[0].name + "附近", j = d.pois[0].location
              //不对经纬度进行修改
              // ,j && 
              // (h = parseFloat(j.split(",")[0]), i = parseFloat(j.split(",")[1]))
            ),
            e.provice && f.push(e.provice), e.city && f.push(e.city), e.district && f.push(e.district), e.streetNumber && e.streetNumber.street && e.streetNumber.number ? (f.push(e.streetNumber.street), f.push(e.streetNumber.number))

            :
            (k = "", d && d.roads[0] && d.roads[0].name && (k = d.roads[0].name), f.push(k)), f = f.join(""),
            //返回的数据,data为定位器数据
            l = [{
              iconPath: a.iconPath,
              width: a.iconWidth,
              height: a.iconHeight,
              name: f, //该地点名称 渤海大道21hao
              desc: g, //地图信息栏下面的描述 曹妃甸校区附近
              longitude: h,
              latitude: i,
              id: 0,
              regeocodeData: d
            }], a.success(l)) : a.fail({
            errCode: b.data.infocode,
            errMsg: b.data.info
          })
        },
        fail: function (b) {
          a.fail({
            errCode: "0",
            errMsg: b.errMsg || ""
          })
        }
      })
    }

    var b = this;
    //a的位置非空，直接调用c(a)。如果a的位置为null，则先获取位置再调用c(a)
    // a.location ? c(a.location) : b.getWxLocation(a, function (a) {
    //   c(a)
    // })
    // console.log(a)
    a.location ? c(a.location) :
      b.getWxLocation(a, function (a) {
        c(a)
      })
  },

  AMapWX.prototype.getPoiAround = function (a) {
    function d(d) {
      var e = {
        key: b.key,
        location: d,
        s: c.s,
        platform: c.platform,
        appname: b.key,
        sdkversion: c.sdkversion,
        logversion: c.logversion
      };
      a.querytypes && (e["types"] = a.querytypes), a.querykeywords && (e["keywords"] = a.querykeywords), wx.request({
        url: "https://restapi.amap.com/v3/place/around",
        data: e,
        method: "GET",
        header: {
          "content-type": "application/json"
        },
        success: function (b) {
          var c, d, e, f;
          if (b.data.status && "1" == b.data.status) {
            if (b = b.data, b && b.pois) {
              for (c = [], d = 0; d < b.pois.length; d++) e = 0 == d ? a.iconPathSelected : a.iconPath, c.push({
                latitude: parseFloat(b.pois[d].location.split(",")[1]),
                longitude: parseFloat(b.pois[d].location.split(",")[0]),
                iconPath: e,
                width: 22,
                height: 32,
                id: d,
                name: b.pois[d].name,
                address: b.pois[d].address
              });
              f = {
                markers: c,
                poisData: b.pois
              }, a.success(f)
            }
          } else a.fail({
            errCode: b.data.infocode,
            errMsg: b.data.info
          })
        },
        fail: function (b) {
          a.fail({
            errCode: "0",
            errMsg: b.errMsg || ""
          })
        }
      })
    }
    var b = this,
      c = b.requestConfig;
    a.location ? d(a.location) : b.getWxLocation(a, function (a) {
      d(a)
    })
  },

  AMapWX.prototype.getStaticmap = function (a) {
    function f(b) {
      c.push("location=" + b), a.zoom && c.push("zoom=" + a.zoom), a.size && c.push("size=" + a.size), a.scale && c.push("scale=" + a.scale), a.markers && c.push("markers=" + a.markers), a.labels && c.push("labels=" + a.labels), a.paths && c.push("paths=" + a.paths), a.traffic && c.push("traffic=" + a.traffic);
      var e = d + c.join("&");
      a.success({
        url: e
      })
    }
    var e, b = this,
      c = [],
      d = "https://restapi.amap.com/v3/staticmap?";
    c.push("key=" + b.key), e = b.requestConfig, c.push("s=" + e.s), c.push("platform=" + e.platform), c.push("appname=" + e.appname), c.push("sdkversion=" + e.sdkversion), c.push("logversion=" + e.logversion), a.location ? f(a.location) : b.getWxLocation(a, function (a) {
      f(a)
    })
  },
  AMapWX.prototype.getInputtips = function (a) {
    var b = this,
      c = b.requestConfig,
      d = {
        key: b.key,
        s: c.s,
        platform: c.platform,
        appname: b.key,
        sdkversion: c.sdkversion,
        logversion: c.logversion
      };
    a.location && (d["location"] = a.location), a.keywords && (d["keywords"] = a.keywords), a.type && (d["type"] = a.type), a.city && (d["city"] = a.city), a.citylimit && (d["citylimit"] = a.citylimit), wx.request({
      url: "https://restapi.amap.com/v3/assistant/inputtips",
      data: d,
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function (b) {
        b && b.data && b.data.tips && a.success({
          tips: b.data.tips
        })
      },
      fail: function (b) {
        a.fail({
          errCode: "0",
          errMsg: b.errMsg || ""
        })
      }
    })
  },
  AMapWX.prototype.getDrivingRoute = function (a) {
    var b = this,
      c = b.requestConfig,
      d = {
        key: b.key,
        s: c.s,
        platform: c.platform,
        appname: b.key,
        sdkversion: c.sdkversion,
        logversion: c.logversion
      };
    a.origin && (d["origin"] = a.origin), a.destination && (d["destination"] = a.destination), a.strategy && (d["strategy"] = a.strategy), a.waypoints && (d["waypoints"] = a.waypoints), a.avoidpolygons && (d["avoidpolygons"] = a.avoidpolygons), a.avoidroad && (d["avoidroad"] = a.avoidroad), wx.request({
      url: "https://restapi.amap.com/v3/direction/driving",
      data: d,
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function (b) {
        b && b.data && b.data.route && a.success({
          paths: b.data.route.paths,
          taxi_cost: b.data.route.taxi_cost || ""
        })
      },
      fail: function (b) {
        a.fail({
          errCode: "0",
          errMsg: b.errMsg || ""
        })
      }
    })
  },
  AMapWX.prototype.getWalkingRoute = function (a) {
    var b = this,
      c = b.requestConfig,
      d = {
        key: b.key,
        s: c.s,
        platform: c.platform,
        appname: b.key,
        sdkversion: c.sdkversion,
        logversion: c.logversion
      };
    a.origin && (d["origin"] = a.origin), a.destination && (d["destination"] = a.destination), wx.request({
      url: "https://restapi.amap.com/v3/direction/walking",
      data: d,
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function (b) {
        b && b.data && b.data.route && a.success({
          paths: b.data.route.paths
        })
      },
      fail: function (b) {
        a.fail({
          errCode: "0",
          errMsg: b.errMsg || ""
        })
      }
    })
  },
  AMapWX.prototype.getTransitRoute = function (a) {
    var b = this,
      c = b.requestConfig,
      d = {
        key: b.key,
        s: c.s,
        platform: c.platform,
        appname: b.key,
        sdkversion: c.sdkversion,
        logversion: c.logversion
      };
    a.origin && (d["origin"] = a.origin), a.destination && (d["destination"] = a.destination), a.strategy && (d["strategy"] = a.strategy), a.city && (d["city"] = a.city), a.cityd && (d["cityd"] = a.cityd), wx.request({
      url: "https://restapi.amap.com/v3/direction/transit/integrated",
      data: d,
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function (b) {
        if (b && b.data && b.data.route) {
          var c = b.data.route;
          a.success({
            distance: c.distance || "",
            taxi_cost: c.taxi_cost || "",
            transits: c.transits
          })
        }
      },
      fail: function (b) {
        a.fail({
          errCode: "0",
          errMsg: b.errMsg || ""
        })
      }
    })
  },
  AMapWX.prototype.getRidingRoute = function (a) {
    var b = this,
      c = b.requestConfig,
      d = {
        key: b.key,
        s: c.s,
        platform: c.platform,
        appname: b.key,
        sdkversion: c.sdkversion,
        logversion: c.logversion
      };
    a.origin && (d["origin"] = a.origin), a.destination && (d["destination"] = a.destination), wx.request({
      url: "https://restapi.amap.com/v4/direction/bicycling",
      data: d,
      method: "GET",
      header: {
        "content-type": "application/json"
      },
      success: function (b) {
        b && b.data && b.data.data && a.success({
          paths: b.data.data.paths
        })
      },
      fail: function (b) {
        a.fail({
          errCode: "0",
          errMsg: b.errMsg || ""
        })
      }
    })
  },
  module.exports.AMapWX = AMapWX;